package com.fzx.aidlservice;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.provider.Settings;
import androidx.annotation.Nullable;
import android.util.Log;

import com.fzx.aidlservice.bean.UserInfo;
import com.fzx.aidlservice.callback.ICallBack;
import com.fzx.aidlservice.callback.IObserversCallBack;

import java.util.List;
import java.util.Map;

/**
 * 描述：
 * 项目名称：AidlDemo
 * 创建人：  FangZhixin
 * 创建时间：2018/8/3
 *
 */
public class AidlService extends Service {

    private static String TAG = "fzx";
    //实现服务内主动推送到客户端，独立进程，不能通过静态变量等方式改变
    private final RemoteCallbackList<IObserversCallBack> mCallbackList = new RemoteCallbackList<>();
    private int count;
    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int num = mCallbackList.beginBroadcast();
            for (int i = 0; i < num; i++) {
                try {
                    mCallbackList.getBroadcastItem(i).onReceive(count++ + " 123");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            mCallbackList.finishBroadcast();
            //每2秒推一次
            mHandler.sendEmptyMessageDelayed(1, 2000);
        }
    };
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        mHandler.sendEmptyMessageDelayed(1, 2000);
        return new MyAidlService();
    }

    public class MyAidlService extends IAidlInterface.Stub {

        @Override
        public int add(int a, int b) throws RemoteException {
            return a + b;
        }

        @Override
        public String getString(String res) throws RemoteException {
            return res;
        }

        @Override
        public CharSequence getCharSequence(CharSequence ch) throws RemoteException {
            return ch.toString();
        }

        @Override
        public void gotoSetting() throws RemoteException {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//不加这句出现异常
            startActivity(intent);
        }

        @Override
        public void setUserInfo(UserInfo info) throws RemoteException {
            mUserInfo = info;
        }

        @Override
        public UserInfo getUserInfo() throws RemoteException {
            return mUserInfo;
        }

        @Override
        public void setArrayData(String[] arr) throws RemoteException {
            mArray = arr;
        }

        @Override
        public String[] getArrayData() throws RemoteException {
            return mArray;
        }

        @Override
        public void setDataList(List<String> list) throws RemoteException {
            mList = list;
        }

        @Override
        public List<String> getDataList() throws RemoteException {
            return mList;
        }

        @Override
        public void setUserList(List<UserInfo> userList) throws RemoteException {
            mUserList = userList;
        }

        @Override
        public List<UserInfo> getUserList() throws RemoteException {
            return mUserList;
        }

        @Override
        public void setMap(Map map) throws RemoteException {
            mMap = map;
        }

        @Override
        public Map getMap() throws RemoteException {
            return mMap;
        }

        @Override
        public void testCallback(final ICallBack callback) throws RemoteException {
            if (callback != null) {
                callback.onSuccess("客户端回调获取到的数据");
            }
        }

        @Override
        public void register(final IObserversCallBack callback) throws RemoteException {
            if (callback != null) {
                mCallbackList.register(callback);
            }
        }

        @Override
        public void unregister(final IObserversCallBack callback) throws RemoteException {
            if (callback != null) {
                mCallbackList.unregister(callback);
            }
        }

        @Override
        public void testin(UserInfo info) throws RemoteException {
            Log.i(TAG, "testin: 1 "+info.toString());
            info.setUserId(111);
            info.setUserName("service.fzx.in");
            info.setPassword("service.in11111111");
            Log.i(TAG, "testin: 2 "+info.toString());
        }

        @Override
        public void testout(UserInfo info) throws RemoteException {
            Log.i(TAG, "testout: 1 "+info.toString());
            info.setUserId(222);
            info.setUserName("service.fzx.out");
            info.setPassword("service.out22222222");
            Log.i(TAG, "testout: 2 "+info.toString());
        }

        @Override
        public void testinout(UserInfo info) throws RemoteException {
            Log.i(TAG, "testinout: 1 "+info.toString());
            info.setUserId(333);
            info.setUserName("service.fzx.inout");
            info.setPassword("service.inout333");
            Log.i(TAG, "testinout: 2 "+info.toString());
        }

        @Override
        public void clearServiceData() throws RemoteException {
            mArray = null;
            mUserInfo = null;
            mList = null;
            mUserList = null;
            mMap = null;
        }

        @Override
        public void setFunc() throws RemoteException {

        }

    }

    private String[] mArray;
    private UserInfo mUserInfo;
    private List<String> mList;
    private List<UserInfo> mUserList;
    private Map mMap;

}
